package action;

import org.apache.struts2.ServletActionContext;

import dao.DaoFactory;
import dao.UserDao;
import entity.User;

public class UserAction {
	private User user;
	
	public String logout(){
		ServletActionContext.getPageContext().getSession().invalidate();
		return "logout";
	}
	
	public String update_finduser() throws Exception{
		UserDao dao = DaoFactory.getInstance(UserDao.class);
		user = dao.findById(user.getId());
		return "update";
	}
	
	public String update() throws Exception{
		UserDao dao=DaoFactory.getInstance(UserDao.class);
		dao.update(user);
		return "detail";
	}
	
	public String detail() throws Exception{
		UserDao dao = DaoFactory.getInstance(UserDao.class);
		user = dao.findById(user.getId());
		return "detail";
	}
	
	public String delete() {
		UserDao dao = DaoFactory.getInstance(UserDao.class);
		try {
			dao.delete(user.getId());
			return "userDeleted";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String add(){
		UserDao dao = DaoFactory.getInstance(UserDao.class);
		try {
			dao.save(user);
			return "userAdded";
		} catch (Exception e) {
			e.printStackTrace();
			return "userExisted";
		}
		
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
